(function () {
  'use strict';
  angular.module('controllers.accessSystem.faildTrade', [])
    .controller('faildTradeCtrl', faildTradeCtrl);

  function faildTradeCtrl($scope, faildTradeService, CoreService, CoreLoading, COREDATA, $filter, $mdDialog, conmmonServ) {
    var year, month, day;
    year = new Date().getFullYear();
    month = new Date().getMonth();
    day = new Date().getDate();
    //页面 账期
    $scope.timeTypes = [
      {
        timeTypeCode: '1',
        timeTypeName: '日'
      },{
        timeTypeCode: '3',
        timeTypeName: '月'
      },{
        timeTypeCode: '5',
        timeTypeName: '年'
      }
    ];
    $scope.tradTypes = []; //页面 交易类型
    $scope.tradDataList = [];  //页面失败交易数据list
    $scope.sendData = {};  //导出操作所需参数
    $scope.title = '';  //页面标题
    $scope.timeDesc = '';  //页面标题

    //页面一些图标的显示与隐藏
    $scope.flag = {
      showIcon: false,  //控制显示折线和表格的图标
      showSearchPanel: true,  //显示查询面板
      hideLittleUp: true,  //隐藏查询面板中向上的小箭头
      viewType: null
    };

    //页面查询参数
    $scope.params = {
      timeType: '1',
      startTime: day-1,
      endTime: day-1,
      tradTypeCode: [], //交易类型
      resultcode: null,
      rspcodeheader:null,  //省二级返回码
      transido: null,  //流水号
      isAccess: true   //是否考核 1:是  0:否
    }
    $scope.Access = '考核';
    $scope.params.tradTypeCode = 0;

    //分页参数
    $scope.send = {
      page: {
        currentPage: 1,
        pageSize: '10',
        totalCount: 40
      }
    };
    $scope.pageItems = ['10', '20', '40'];

    //监控帐期，选择不同的帐期，给开始时间和结束时间不同的默认值
    $scope.$watch('params.timeType', function (newValue) {
      switch (newValue) {
        case '5':
          $scope.params.startTime = year-1;
          $scope.params.endTime = year-1;
          break;
        case '4':
          $scope.params.startTime = '';
          $scope.params.endTime = '';
          break;
        case '3':
          $scope.params.startTime = new Date(year, month-1);
          $scope.params.endTime = new Date(year, month-1);
          break;
        case '1':
          $scope.params.startTime = new Date(year, month, day-1);
          $scope.params.endTime = new Date(year, month, day-1);
      }
    });

    //隐藏查询面板
    $scope.hideSearchPanel = function () {
      $scope.flag.showSearchPanel = false;
    };
    //页面初始化 交易类型下拉框数据加载
    $scope.initQuery = function () {
      faildTradeService.queryTradeType(function (res) {
        if(res.data.length>0){
          $scope.tradTypes = res.data;
        }
      });
    }
    //改变 是否考核按钮
    $scope.changeAccess = function () {
      if ($scope.params.isAccess) {
        $scope.Access = '考核';
      } else {
        $scope.Access = '不考核';
      }
    };

    //查询
    var param= {};;
    $scope.search = function (isPage) {
      $scope.flag.hideLittleUp = false;
      //收起查询面板
      if (!$scope.flag.showSearchPanel && !isPage) {
        $scope.flag.showSearchPanel = true;
        return;
      }
      /*$scope.params.isAccess ? $scope.params.isAccess = 1 : $scope.params.isAccess = 0;*/
      param = angular.copy($scope.params);
      param.isAccess ? param.isAccess = 1 : param.isAccess = 0;
      param.pageSize = parseInt($scope.send.page.pageSize);
      if($scope.showSearchPanel){
        param.currentPage = 1;
      }else{
        param.currentPage = $scope.send.page.currentPage;
      }

      if (param.timeType === '5') {
        param.startTime = param.startTime.toString();
        param.endTime = param.endTime.toString();

        $scope.timeDesc = param.startTime+' 至 '+param.endTime;
      } else if (param.timeType === '3') {
        param.startTime = $filter('date')(param.startTime, 'yyyy-MM');
        param.endTime = $filter('date')(param.endTime, 'yyyy-MM');

        $scope.timeDesc = param.startTime+' 至 '+param.endTime;
      } else {
        param.startTime = $filter('date')(param.startTime, 'yyyy-MM-dd');
        param.endTime = $filter('date')(param.endTime, 'yyyy-MM-dd');

        $scope.timeDesc = param.startTime+' 至 '+param.endTime;
      }
      var tradTypeName ='';
      if(param.tradTypeCode != 0){
        $scope.tradTypes.map(function (obj) {
          if(obj.tradeTypeCode==param.tradTypeCode){
            tradTypeName = obj.tradeTypeName;
          }
        });
      }
      param.tradTypeCode = param.tradTypeCode != 0 ? [param.tradTypeCode] : $scope.tradTypes.map(function (obj) {
        return obj.tradeTypeCode;
      });
      $scope.title= $scope.timeDesc+''+tradTypeName+' 期间失败交易统计';

      $scope.sendData=angular.copy(param);
      CoreLoading.setState(true);
      faildTradeService.query(param, function (res) {
        CoreLoading.setState(false);
        $scope.flag.showSearchPanel = false;
        if (res.state === '10000' && res.data.resultList.length >= 1) {
          $scope.tradDataList = res.data.resultList;
          $scope.send.page.currentPage = res.data.currentPage;
          $scope.send.page.totalCount = res.data.totalCount;
          $scope.send.page.pageSize = res.data.pageSize + '';
          $scope.flag.viewType = 'table';
          $scope.flag.showIcon = true;
        } else {
          CoreService.dialog('查询无数据');
          $scope.pageStyles();
        }

      });
    }

    //查询无数据 页面样式
    $scope.pageStyles = function () {
      $scope.tradDataList = null;
      $scope.flag.viewType = null;
      $scope.flag.showIcon = false;
      $scope.flag.showSearchPanel = true;
    }

    //导出路径
    $scope.formPrefix = COREDATA.FORMPREFIX + 'accessSystem/failTrade/export';




    /*$scope.all = false;     //全选复选框
    $scope.item = false;   //单选 复选框
    $scope.tradInfoCodes = [];

    //页面全选 复选框
    $scope.checkAll = function () {
      $scope.tradDataList.forEach(function (data) {
        data.checked = $scope.all;
      });
    }

    //页面单选 复选框
    $scope.checkItem = function () {
      var itemList = [];
      $scope.tradDataList.forEach(function (data) {
        if (data.checked) {
          itemList.push(data.orderNum);
        }
      });
      if (itemList.length === $scope.tradDataList.length) {
        $scope.all = true;
      } else {
        $scope.all = false;
      }
    }


    $scope.$on('updateSuccess', function () {
      $scope.flag.showSearchPanel=true;
      $scope.all = false;
      $scope.search();
    })

    //点击批量修改按钮 显示修改窗口
    $scope.update = {};
    $scope.updateViews = function (data) {
      debugger;
      var templist = [];
      if(typeof(data)!=='undefined'){
        if(!data.checked){
          CoreService.dialog('此条数据未选中,不能修改！');
          return;
        }
        //if (data.orderNum !== undefined) {
        templist.push(data.orderNum);
        //}
      }else {
        $scope.tradDataList.forEach(function (data) {
          if (data.checked) {
            templist.push(data.orderNum);
          }
        });
      }
      if (templist.length === 0) {
        CoreService.dialog('请选择要修改的数据');
        return;
      }
      $scope.update = {
        isAccess: '1',
        reason: '',
        tradInfoCodes: templist
      };
      conmmonServ.setEvn($scope.update);
      $mdDialog.show({
        templateUrl: 'views/accessSystem/faildTrad/update.html',
        /!*targetEvent: ev,*!/
        controller: tradeUpdateCtrl
      });

    };

    //修改窗口的控制器
    function tradeUpdateCtrl($mdDialog, $scope, conmmonServ, faildTradeService, $rootScope) {
      var param = conmmonServ.getEvn();  //从公共的服务取到传给后端的参数
      $scope.update = angular.copy(param);
      //点击确认执行方法
      $scope.confirm = function () {
        faildTradeService.update($scope.update, function (res) {
          if (res.state === '10000') {
            $rootScope.$broadcast('updateSuccess', []);
            CoreService.dialog(res.message);
          }
        });
        $mdDialog.cancel();
      };
      //点击取消执行的操作
      $scope.cancel = function () {
        $mdDialog.cancel();
      };
    };*/


    //$scope.search();  //第一次访问自动查询

  }

  faildTradeCtrl.$inject = ['$scope', 'faildTradeService', 'CoreService', 'CoreLoading', 'COREDATA', '$filter', '$mdDialog', 'conmmonServ'];
})();